<?php
      #防止乱码
      header("content-type:text/json;charset=utf8;");
      header("Access-Control-Allow-Origin:*");

      // 如果不传递对应的参数,会报错;
      // 有办法消除get的报错;
      $usr = @$_GET["username"];
      $pwd = @$_GET["password"];
      // 如果没有传递参数,给前端发送报错信息; 
      if(!$usr || !$pwd){
            $err_msg = array("state"=>"error","msg" => "参数不全");
            die(json_encode($err_msg));
            // die("{\"state\" => \"error\"}")
      }
      // 数据库的插入操作;
      
      $con = mysql_connect("localhost","root","root");
      if(!$con){
            die("数据库连接失败".mysql_error());
      }
      mysql_select_db("zbird",$con);

      #判断一下现在的用户名是否存在于数据库之中;
      $sql_select = "SELECT `password` FROM `user_table` WHERE `username`='$usr'";
      
      $select_res = mysql_query($sql_select);
      $select_res = mysql_fetch_array($select_res);

      if(!!$select_res){
            mysql_close($con);
            $err_msg = array("state"=>"error","msg" => "用户名重名");
            die(json_encode($err_msg));
      }
      
      $sql_insert = "INSERT INTO `user_table` (`username`,`password`)
            VALUES ('$usr','$pwd');
      ";
      $insert_res = mysql_query($sql_insert);

      if(!$insert_res){
            mysql_close($con);
            $err_msg = array("state"=>"error","msg" => "数据库错误".mysql_error());
            die(json_encode($err_msg));
      }
      $succ_msg = array("state"=>"success");
      
      echo json_encode($succ_msg);

      // echo $select_res;

      mysql_close($con);
?>